import { VantResolver } from '@vant/auto-import-resolver'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import type { ConfigEnv, UserConfig } from 'vite'
import { loadEnv } from 'vite'
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir)
}
export default ({ mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  console.log('env', env)
  return {
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    base: '/',
    resolve: {
      // 导入以下文件可以不用后缀名
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
      // 配置路径别名
      alias: [
        // /@/xxxx => src/xxxx
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
        // /#/xxxx => types/xxxx
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
        {
          find: /@\//,
          replacement: pathResolve('src') + '/',
        },
        {
          find: /#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
    },
    server: {
      // 配置代理
      proxy: {
        '^/pc/visitor/fs': {
          /* 目标代理服务器地址 */
          target: 'http://117.160.207.160:5043',
          /* 允许跨域 */
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/pc\/visitor\/fs/, 'pc/fs'),
        },
        '^/hollyics-fs': {
          /* 目标代理服务器地址 */
          target: 'http://117.160.207.160:5043',
          /* 允许跨域 */
          changeOrigin: true,
        },
        '^/pc/visitor/csplat': {
          target: 'http://117.160.207.160:5043', //设置代理目标
          changeOrigin: true, //是否改变请求源地址
          rewrite: (path) =>
            path.replace(/^\/pc\/visitor\/csplat/, '/pc/csplat'),
        },
        '^/pc/visitor': {
          target: 'http://117.160.207.160:5043', //设置代理目标
          changeOrigin: true, //是否改变请求源地址
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "${resolve(__dirname, 'src/assets/styles/global.less')}";`,
        },
      },
    },
  }
}
